mui.init({
	keyEventBind: {
		backbutton: false, //Boolean(默认true)关闭back按键监听,从首页跳转过来时防止回到首页
	},
});

document.getElementById("forgetPassword").addEventListener('tap', function() {
	mui.openWindow({
		url: 'forget_password.html',
		id: 'forget_password'
	});
})

document.getElementById("login").addEventListener('tap', function() {
	var check = true,
		username = document.getElementById("username").value,
		password = document.getElementById("password").value;

	mui("input").each(function() {
		//若当前input为空，则alert提醒 
		if(!this.value || this.value.trim() == "") {
			var label = this.previousElementSibling;
			mui.toast(label.innerText + "不允许为空");
			check = false;
			return false;
		}
	});
	//校验通过，继续执行业务逻辑
	if(check) {
		mui.ajax('http://app.xiaoweicanting.com/hospital.php?m=App&c=login&a=signin', {
			data: {
				username: username,
				password: password,
				type: 'employee'
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'  //修改
			},
			beforeSend: function() {
				document.getElementById("login").setAttribute('disabled', true);
			},
			success: function(data) {
				console.log(data.state)
				if(data.state === 'success') {
					mui.toast(data.msg);
					saveData(data.userinfo);
					//登陆后触发自定义事件，更新数据
					var mw = plus.webview.getWebviewById('my.html');
					var aw = plus.webview.getWebviewById('app.html');
					mui.fire(mw, 'loadMyPage', {});
					mui.fire(aw, 'loadAppPage', {});

					mui.openWindow({
						url: '../../index.html',
						id: 'app.html',
						styles: {
							top: 0, //新页面顶部位置
							bottom: 0, //新页面底部位置
						},
					})
				} else {
					document.getElementById("login").removeAttribute('disabled');
					mui.alert(data.msg);
				}
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				mui.toast('网络异常');
			}
		})
	}
})

function saveData(userData) {
	var dataStr = JSON.stringify(userData);
	plus.storage.setItem("userinfo", dataStr);
}